package com.pgytesting.secondhandcar.service.impl;

import com.pgytesting.secondhandcar.dao.CreditBureauBlackLibraryDao;
import com.pgytesting.secondhandcar.dao.CreditInformationDao;
import com.pgytesting.secondhandcar.dao.McpUserInfoDao;
import com.pgytesting.secondhandcar.dao.OrderNameDao;
import com.pgytesting.secondhandcar.model.CreditBureauBlackLibrary;
import com.pgytesting.secondhandcar.model.CreditInformation;
import com.pgytesting.secondhandcar.model.McpUserInfo;
import com.pgytesting.secondhandcar.model.OrderName;
import com.pgytesting.secondhandcar.service.CreditInformationService;
import com.pgytesting.secondhandcar.util.SessionUtil;
import com.pgytesting.secondhandcar.util.UtilMethod;
import com.pgytesting.secondhandcar.vo.Parameters;
import com.pgytesting.secondhandcar.vo.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户征信(CreditInformation)表服务实现类
 *
 * @author makejava
 * @since 2019-08-12 18:55:58
 */
@Slf4j
@Service("creditInformationService")
public class CreditInformationServiceImpl implements CreditInformationService {
    @Resource
    private CreditInformationDao creditInformationDao;

    @Resource
    private OrderNameDao orderNameDao;

    @Resource
    private McpUserInfoDao mcpUserInfoDao;

    @Resource
    private CreditBureauBlackLibraryDao creditBureauBlackLibraryDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CreditInformation queryById(String id) {
        return this.creditInformationDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<CreditInformation> queryAllByLimit(int offset, int limit) {
        return this.creditInformationDao.queryAllByLimit(offset, limit);
    }

    /**
     * 条件查询多条数据
     *
     * @param creditInformation 实例对象
     * @return 对象列表
     */
    @Override
    public List<CreditInformation> queryAll(CreditInformation creditInformation) {
        return this.creditInformationDao.queryAll(creditInformation);
    }

    /**
     * 新增数据
     *
     * @param creditInformation 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public ResponseMessage insert(CreditInformation creditInformation) {
        // 获取订单号
        String orderId = creditInformation.getId();
        OrderName orderName = orderNameDao.queryById(orderId);
        if (ObjectUtils.isEmpty(orderName)) {
            return ResponseMessage.errorResponse("订单信息查询失败，请确认订单号是否正确");
        }
        // 获取到用户ID
        String userNumber = SessionUtil.getSessionMcpUser().getUserNumber();
        McpUserInfo mcpUserInfo = mcpUserInfoDao.queryById(userNumber);
        // 进行用户手机号码验证
        if (!creditInformation.getPhoneNumber().equals(mcpUserInfo.getPhoneNumber())) {
            return ResponseMessage.errorResponse("用户信息验证失败，请确认手机号码是账号绑定手机号，或者请重新登陆");
        }
        // 准备授权信息
        creditInformation.setCreatedBy(mcpUserInfo.getPhoneNumber());
        creditInformation.setId(UtilMethod.getUniqueCode());
        creditInformation.setUserNumber(mcpUserInfo.getUserNumber());
        // 准备订单数据
        OrderName orderName1 = new OrderName();
        orderName1.setId(orderId);
        orderName1.setUpdatedBy(mcpUserInfo.getPhoneNumber());
        orderName1.setCreditInformationId(creditInformation.getId());
        // 进行征信黑库比对，存在则失败，不存在则成功
        CreditBureauBlackLibrary blackLibrary = new CreditBureauBlackLibrary();
        blackLibrary.setUntrustedPersonsName(mcpUserInfo.getUserName());
        blackLibrary.setUntrustedPersonIdNumber(mcpUserInfo.getIdentificationNumber());
        Integer count = creditBureauBlackLibraryDao.queryCount(blackLibrary);
        // 进行征信黑库比对，存在则失败，不存在则成功
        // 如果是驳回类的，则显示已上传待运营商审核
        if (count > 0) {
            orderName1.setOrderStatus(Parameters.ORDER_24002);
        } else {
            orderName1.setOrderStatus(Parameters.ORDER_24001);
        }
        log.info("进行征信授权信息新增");
        creditInformationDao.insert(creditInformation);
        log.info("订单信息更新");
        orderNameDao.update(orderName1);
        return ResponseMessage.successResponse("上传征信授权信息成功，等待审核");
    }

    /**
     * 修改数据
     *
     * @param creditInformation 实例对象
     * @return 实例对象
     */
    @Override
    public CreditInformation update(CreditInformation creditInformation) {
        this.creditInformationDao.update(creditInformation);
        return this.queryById(creditInformation.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.creditInformationDao.deleteById(id) > 0;
    }
}